var ThreeLevelController = Class.create();
Object.extend(ThreeLevelController.prototype, Observable);
Object.extend(ThreeLevelController.prototype, {
	initialize : function(_sel1, _sel2, _sel3, areaJson, prov, city, dist){
		this._observable(['firstOnchange', 'secondOnchange']);
		this.sel1 = ff(_sel1);
		if(!this.sel1) this.sel1 = _sel1;
		this.sel1.style.width = '60px';
		this.sel1.onchange = this.firstOnchange.bindAsEventListener(this);
		this.sel2 = ff(_sel2);
		if(!this.sel2) this.sel2 = _sel2;
		this.sel2.style.width = '60px';
		this.sel2.onchange = this.secondOnchange.bindAsEventListener(this);
		this.sel3 = ff(_sel3);
		if(!this.sel3) this.sel3 = _sel3;
		this.sel3.style.width = '60px';
		//this.sel3.onchange = this.thirdOnchange.bindAsEventListener(this);
		this.areaJson = areaJson;

		this.prov = prov;
		this.city = city;
		this.dist = dist;
	},
	firstOnchange : function() {
		this.triggerEvent('firstOnchange', [this]);
	},
	secondOnchange : function() {
		this.triggerEvent('secondOnchange', [this]);
	}
});

/**
 * _sel1 省份 下拉框id
 * _sel2 城市 下拉框id
 * _sel3 区县 下拉框id
 * areaJson  数据封装对象
 * prov 默认省份
 * city 默认城市
 * dist 默认区县
 *  例如：
 *	var areaJson = {'all':{'provinces':[{},{}]}};
 *	new LevelMenu('id1', 'id2', 'id3', areaJson);
 */
var LevelMenu = Class.create();
Object.extend(LevelMenu.prototype, {
	initialize : function(_sel1, _sel2, _sel3, areaJson, prov, city, dist){
		this.ctr = new ThreeLevelController(_sel1, _sel2, _sel3, areaJson, prov, city, dist);
		this.ctr.menu = this;
		this.initFirstElems.bind(this.ctr)();
		this.initSecElems.bind(this.ctr)();
		this.initThdElems.bind(this.ctr)();

		this.ctr.addListener('firstOnchange', function(){
			this.menu.initThdElems.bind(this)();
			this.menu.changeSecElems.bind(this)();
		}.bind(this.ctr));

		this.ctr.addListener('secondOnchange', function(){
			this.menu.changeThdElems.bind(this)();
		}.bind(this.ctr));

	},
	initFirstElems : function() {
		var provinces = this.areaJson.sector.provinces;
		var len = provinces.length;
		for (i = 0; i < len ; i++) {
			var opt = new Option(provinces[i].name, provinces[i].value);
			opt.province = provinces[i];
			this.sel1.options[this.sel1.length] = opt;
			if (opt.text == this.prov) {
				opt.selected = true;
				//this.prov = null;
			}
		}
	},
	initSecElems : function() {
		if (this.prov)
		{
			this.menu.changeSecElems.bind(this)();
		} 
		else {
			this.menu.removeElems(this.sel2);
			var opt2 = new Option('请选择',-3);
			this.sel2.options[0] = opt2;
		}
	},
	initThdElems : function() {
		if (this.city)
		{
			this.menu.changeThdElems.bind(this)();
		} 
		else {
			this.menu.removeElems(this.sel3);
			var opt3 = new Option('请选择',-4);
			this.sel3.options[0] = opt3;
		}
	},
	removeElems : function(sel) {
		while (0 < sel.length) {
			sel.options[0] = null;
		}
	},
	changeSecElems : function() {
		this.menu.removeElems(this.sel2);
		this.sel2.cities = this.sel1.options[this.sel1.options.selectedIndex].province.cities;
		var cities = this.sel2.cities;
		var len = cities.length;
		for (i = 0 ; i < len; i++) {
			var opt = new Option(cities[i].name, cities[i].value);
			opt.city = cities[i];
			this.sel2.options[this.sel2.length] = opt;
			if (this.city == opt.text)
			{
				opt.selected = true;
				//this.city = null;
			}
		}
	},
	changeThdElems : function() {
		this.menu.removeElems(this.sel3);
		this.sel3.districts = this.sel2.options[this.sel2.options.selectedIndex].city.districts;
		var districts = this.sel3.districts;
		var len = districts.length;
		for (i = 0 ; i<len; i++) {
			var opt = new Option(districts[i].name, districts[i].value);
			opt.district = districts[i];
			this.sel3.options[this.sel3.length] = opt;
			if (this.dist == opt.text)
			{
				opt.selected = true;
				this.dist = null;
				this.city = null;
				this.prov = null;
			}
		}
	},
	/**
	 *  -1 选择了省 ；0 选择了市； 正整数 选择了区县
	 *  返回值 ：-2 未选择省 -3 未选择市 -4 未选择区县
	 */
	getValue : function() {
		if (this.ctr.sel1.value == -1)
		{
			if (this.ctr.sel2.value == 0)
			{
				return this.ctr.sel3.value;
			} 
			else 
			{
				return -3;
			}
		} 
		else 
		{
			return -2;
		}
	}
});
